/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Fri 23 Nov 2018 12:06:48 PM CST
 * Destription: 测试acl的json组成
 *
 */
#include <acl-lib/acl/lib_acl.h>
#include <acl-lib/acl_cpp/lib_acl.hpp>

/**
 * 检测node是否是数组节点
 * @return true 是数组，false 非数组
 */
bool check_array_node(acl::json_node * node) {
    if(NULL==node) {
        return false;
    }
    acl::json_node * tmpnode = node->get_obj();
    if(NULL==tmpnode || false==tmpnode->is_array()) {
        return false;
    }
    return true;
}

/**
 * 检测node节点或节点内容是否为空内容
 * @param node 要检测的节点
 * @return true 内容为空或节点为空，false 内容不为空
 */
bool check_empty_content(acl::json_node * node) {
    if(NULL==node || NULL==node->get_text() || 0==strcmp("",node->get_text())) {
        return true;
    }
    return false;
}

bool deal_one_view(acl::json_node & node) {
    acl::json_node * nodeValue = node["ttl"];
    if(NULL==nodeValue) {
        return false;
    }
    long long ttl = *nodeValue->get_int64();
    nodeValue = node["class"];
    if(NULL==nodeValue) {
        return false;
    }
    std::string cl(nodeValue->get_string());
    nodeValue = node["type"];
    if(NULL==nodeValue) {
        return false;
    }
    std::string type(nodeValue->get_string());
    logger("ttl=%lld cl=%s type=%s", ttl, cl.c_str(), type.c_str());

    acl::json_node * nodeArray = node["rdata"];
    if(!check_array_node(nodeArray)) {
        return false;
    }
    std::set<std::string> rvalues;
    {
        acl::json_node * first = nodeArray->first_child();
        acl::json_node * child = first->first_child();
        while (child) {
            printf("tag_name=%s type=%s, text=%s, \n", child->tag_name(), child->get_type(), child->get_text());
            if(false==check_empty_content(child)) {
                rvalues.insert(child->get_text());
            }
            child = first->next_child();
        }
    }
    nodeArray = node["view"];
    if(!check_array_node(nodeArray)) {
        return false;
    }
    {
        acl::json_node * first = nodeArray->first_child();
        acl::json_node * child = first->first_child();
        while (child) {
            if(false==check_empty_content(child)) {
                std::set<std::string>::iterator it = rvalues.begin();
                for(; it!=rvalues.end(); ++it) {
                    printf("    line=%lld   %s    %s    %s    %s\n",
                            ttl, cl.c_str(), type.c_str(), (*it).c_str(), child->get_text());
                }
            }
            child = first->next_child();
        }
    }
}

void test_parse_json2() {
    acl::json root("{\"taskid\":12345678,\"content\":[ \
            {\"ttl\": 600, \"class\":\"IN\", \"type\":\"A\", \"rdata\":[\"113.207.49.142\", \"1.2.3.4\"], \"view\":[\"v1\", \"v2\"]}, \
            {\"ttl\": 500, \"class\":\"IN\", \"type\":\"A\", \"rdata\":[\"2.3.4.5\", \"1.2.3.4\"], \"view\":[\"v3\", \"v4\"]} \
            ] \
            }");
    acl::json_node * nodeContent = root.getFirstElementByTags("content");
    acl::json_node * first = nodeContent->first_child();
    if(NULL==first) {
        logger("first is NULL");
        return;
    }
    printf("first type=%s\r\n", first->get_type());
    acl::json_node* child = first->first_child();
    while (child) {
        deal_one_view(*child);
        child = first->next_child();
    }
}

void test_parse_json()
{
    acl::json root("{\"uri\":\"/videos/v0/20190423/63/d0/0c8b1841996a1d9c5042c21d5fd7d3e6.265ts\",\"vidcs\":[\"iocp_hunan_iqiyi_ct\",\"iocp_hunan_iqiyi_cnc\"]}");
    acl::json_node * nodeUri = root.getFirstElementByTags("uri");
    if(NULL==nodeUri || NULL==nodeUri->get_text()) {
        logger("not parse uri");
        return;
    } else {
        logger("uri=%s", nodeUri->get_text());
    }
    acl::json_node * nodeVidc = root.getFirstElementByTags("vidcs");
    acl::json_node * first = nodeVidc->first_child();
    if(NULL==first) {
        logger("first is NULL");
    } else {
        printf("first type=%s\r\n", first->get_type());
        acl::json_node* child = first->first_child();
        while (child) {
            printf("first=%s, text=%s\n", child->get_type(), child->get_text());
            child = first->next_child();
        }
    }
    logger("text=%s\n", nodeVidc->get_text());
}

int main(int argc, char *argv[])
{
    acl::log::stdout_open(true);
    test_parse_json2();
    return 0;
    acl::json json;
    acl::json_node& root = json.get_root();

    root.add_text("cmd", "delcontent").add_text("uuid", "xyz");
    acl::json_node *node1, *node2;
    acl::json_node *content = &json.create_node(true);
    node1 = &json.create_node("content", content);
    root.add_child(node1);

    node2 = &json.create_node();
    node2->add_text("aaa", "bbb");
    node2->add_text("aaa", "bbb");
    node1 = &json.create_node();
    node1->add_text("aaa", "bbb");
    content->add_child(node2).add_child(node1);

//    content->add_child(node2);


    /*
    node0 = &json.create_node(true);
    node1 = &json.create_node("content", node0);
    root.add_child(node1);

    node2 = &json.create_node("name3", "value3");
    node1->add_child(node2);
    node2 = &json.create_node("name4", node1);
    node1->add_child(node2);
    */

    printf("-------------------------------------------------\r\n");

    acl::string buf;
    json.build_json(buf);

    printf("-----------------json------------------------\r\n");
    printf("%s\r\n", buf.c_str());
    return 0;
}
